-
Notifications
You must be signed in to change notification settings - Fork 65
Thread parking for Kotlin/Common #498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
atomicfu/src/jvmMain/kotlin/kotlinx/atomicfu/parking/KThread.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/jvmMain/kotlin/kotlinx/atomicfu/parking/JvmParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/jvmMain/kotlin/kotlinx/atomicfu/parking/JvmParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/androidNative32BitMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/commonMain/kotlin/kotlinx/atomicfu/parking/ParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/jvmMain/kotlin/kotlinx/atomicfu/parking/JvmParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/androidNative32BitMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/androidNative64BitMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/androidNative32BitMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/androidNative32BitMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/jsAndWasmSharedMain/kotlin/kotlinx/atomicfu/parking/KThread.kt
Outdated
Show resolved
Hide resolved
b82d7aa
to
22d9ca4
Compare
atomicfu/src/concurrentMain/kotlin/kotlinx/atomicfu/parking/ThreadParker.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/concurrentMain/kotlin/kotlinx/atomicfu/parking/ThreadParker.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/jvmMain/kotlin/kotlinx/atomicfu/parking/JvmParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/jvmMain/kotlin/kotlinx/atomicfu/parking/KThread.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/androidNative32BitMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/mingwMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/androidNativeMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/jvmMain/kotlin/kotlinx/atomicfu/parking/JvmParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/mingwMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/nativeUnixLikeMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/concurrentMain/kotlin/kotlinx/atomicfu/parking/ParkingDelegator.kt
Outdated
Show resolved
Hide resolved
val nanos = Random.nextLong() | ||
val updatedTime = currentTimeInSeconds.addNanosToSeconds(nanos) | ||
if (nanos > 0) assertTrue { updatedTime > currentTimeInSeconds } | ||
if (nanos < 0) assertTrue { updatedTime < currentTimeInSeconds } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably makes more sense to assert in the code that the negative number of nanos never even enters addNanosToSeconds
. No reason to test the code path that will never be taken.
atomicfu/src/jvmMain/kotlin/kotlinx/atomicfu/parking/JvmParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/nativeTest/kotlin/kotlinx/atomicfu/parking/TestThread.native.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/nativeMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
internal inline fun callAndVerifyNative(vararg expectedReturn: Int, block: () -> Int): Int { | ||
val returnStatus = block() | ||
if (expectedReturn.all { it != returnStatus }) | ||
throw IllegalStateException("Calling native, expected one return status of ${expectedReturn.joinToString(", ")}, but was $returnStatus") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd also print the contents of https://www.man7.org/linux/man-pages/man3/errno.3.html
atomicfu/src/mingwMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/concurrentTest/kotlin/kotlinx/atomicfu/parking/TimeArithmeticTests.kt
Outdated
Show resolved
Hide resolved
val currentTimeInInt = currentTimeInSeconds.toInt() | ||
val nanos = Random.nextLong() | ||
val updatedTime = currentTimeInInt.addNanosToSeconds(nanos) | ||
if (nanos > 0) assertTrue { updatedTime > currentTimeInSeconds && updatedTime <= Int.MAX_VALUE } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A stronger version of the check: if the value does not clamp, the (updatedTime - currentTime) == nanos / 1_000_000_000
; if the value clamps, it's because as a Long
value, it would exceed an Int
. This describes what we want from addNanosToSeconds
more precisely. As is, when I write internal fun Long.addNanosToSeconds(nanos: Long): Long = this + nanos / 2_000_000_000
, for example, no tests fail.
atomicfu/src/concurrentTest/kotlin/kotlinx/atomicfu/parking/TimedParkingTest.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/concurrentTest/kotlin/kotlinx/atomicfu/locks/TestThread.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/concurrentTest/kotlin/kotlinx/atomicfu/locks/ThreadParkerTest.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/androidNativeMain/kotlin/kotlinx/atomicfu/locks/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/androidNativeMain/kotlin/kotlinx/atomicfu/locks/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/mingwMain/kotlin/kotlinx/atomicfu/locks/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/nativeMain/kotlin/kotlinx/atomicfu/locks/NativeParkingUtils.kt
Outdated
Show resolved
Hide resolved
actual fun createRef(): ParkingData { | ||
val mut = nativeHeap.alloc<pthread_mutex_t>().ptr | ||
val cond = nativeHeap.alloc<pthread_cond_t>().ptr | ||
val attr = nativeHeap.alloc<pthread_condattr_tVar>().ptr |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: this is irritatingly close to pthread_condattr_t
, which is used by the other Unix-like platforms, but after several attempts, I did not manage to find an incantation that would allow avoiding the code duplication for androidNativeMain
.
atomicfu/src/concurrentTest/kotlin/kotlinx/atomicfu/locks/ExchangerTest.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/concurrentTest/kotlin/kotlinx/atomicfu/locks/ThreadParkerTest.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/concurrentTest/kotlin/kotlinx/atomicfu/locks/ThreadParkerTest.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/concurrentTest/kotlin/kotlinx/atomicfu/locks/ThreadParkerTest.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/concurrentTest/kotlin/kotlinx/atomicfu/locks/TimedParkingTest.kt
Outdated
Show resolved
Hide resolved
This comment was marked as resolved.
This comment was marked as resolved.
There are small formatting issues here and there (like trailing spaces), please run the "Code/Reformat Code" in IJ (you can perform a bulk cleanup by selecting all the necessary files and then running the reformatting). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nicely done! I left a few comments regarding tests and documentation, but overall it looks good.
atomicfu/src/concurrentMain/kotlin/kotlinx/atomicfu/locks/ThreadParker.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/concurrentMain/kotlin/kotlinx/atomicfu/locks/ParkingSupport.kt
Show resolved
Hide resolved
atomicfu/src/concurrentMain/kotlin/kotlinx/atomicfu/locks/ParkingSupport.kt
Show resolved
Hide resolved
atomicfu/src/concurrentMain/kotlin/kotlinx/atomicfu/locks/ParkingSupport.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/concurrentMain/kotlin/kotlinx/atomicfu/locks/ParkingSupport.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/concurrentMain/kotlin/kotlinx/atomicfu/locks/ParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/concurrentMain/kotlin/kotlinx/atomicfu/locks/ParkingSupport.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/concurrentTest/kotlin/kotlinx/atomicfu/locks/TimedParkingTest.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/nativeUnixLikeMain/kotlin/kotlinx/atomicfu/locks/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/concurrentTest/kotlin/kotlinx/atomicfu/locks/TimedParkingTest.kt
Outdated
Show resolved
Hide resolved
519e59d
to
0d9ea07
Compare
…`, try/finally for pthread_mutex_unlock,
…me asserts, cover `parkUntil`
atomicfu/src/concurrentTest/kotlin/kotlinx/atomicfu/locks/CyclicBarrierTest.kt
Outdated
Show resolved
Hide resolved
809689c
to
4cd091b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a few nits, but otherwise the change looks good.
atomicfu/src/concurrentMain/kotlin/kotlinx/atomicfu/locks/ParkingSupport.kt
Show resolved
Hide resolved
atomicfu/src/jvmMain/kotlin/kotlinx/atomicfu/locks/JvmParkingDelegator.kt
Show resolved
Hide resolved
atomicfu/src/concurrentMain/kotlin/kotlinx/atomicfu/locks/ParkingSupport.kt
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, what next? @ndkoval, do you also want to review this, or can we merge the PR?
@dkhalanskyjb please merge the PR. |
Allows to pause and resume thread execution. On native platforms it is based on pthread_cond_wait, on JVM it uses LockSupport.
Threads can be pre unparked (calling unpark before park cancels the parking operation). And thread can be parker with a timeout.
Suspend the current thread by calling:
Resume a thread by calling:
Get current thread reference by calling: